package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IInvoiceMsg;
import com.fingard.dsp.bank.reqrespobj.invoicemsg.ReqInvoiceMsgQry;
import com.fingard.dsp.bank.reqrespobj.invoicemsg.RetAtsInvoiceMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02InvoiceMsg extends CITIC02Base implements IInvoiceMsg {
    @Override
    public RetAtsInvoiceMsg getInvoiceMsg(ReqInvoiceMsgQry qry) throws Exception {
        RetAtsInvoiceMsg retAts = new RetAtsInvoiceMsg(qry.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCINMSM";
//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            HOSTNO	企业端经销商客户代码	VARCHAR	20	N
            xmlWriterBody.writeElementString("HOSTNO",qry.DealerCode);
//            HOSTNAME	借款企业名称	VARCHAR	122	Y	借款企业名称
            xmlWriterBody.writeElementString("HOSTNAME",qry.DealerName);
//            ORDERNO	交易流水号	VARCHAR	20	Y	企业端发票交易流水号(批次号)
            xmlWriterBody.writeElementString("ORDERNO",qry.reqHeader.transSeqID);
//            OPRNO	操作人编号	VARCHAR	20	N
            xmlWriterBody.writeElementString("OPRNO","");
//            OPRNM	操作人名称	VARCHAR	30	N
            xmlWriterBody.writeElementString("OPRNM","");
//            FIELD1	属性1	VARCHAR	30	N
            xmlWriterBody.writeElementString("FIELD1","");
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD3","");
//            FIELD4	属性4	DECIMAL	19,2	N
            xmlWriterBody.writeElementString("FIELD4","");
//            FIELD5	属性5	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD5","");
//            LIST	汽车列表	LIST
            xmlWriterBody.writeStartElement("LST");

            while (qry.readRecord()) {
                xmlWriterBody.writeStartElement("row");
//                VIN	车架号	VARCHAR	20	Y
                xmlWriterBody.writeElementString("VIN",qry.reqInvoiceMsgDetail.VIN);
//                CERTNO	合格证号	VARCHAR	60	N
                xmlWriterBody.writeElementString("CERTNO",qry.reqInvoiceMsgDetail.certificationNo);
//                CLASS	车型	VARCHAR	100	N
                xmlWriterBody.writeElementString("CLASS",qry.reqInvoiceMsgDetail.CarTypeCode);
//                COLOUR	颜色	VARCHAR	20	N
                xmlWriterBody.writeElementString("COLOUR","");
//                ENGINENO	发动机号	VARCHAR	30	N
                xmlWriterBody.writeElementString("ENGINENO","");
//                INVOICEPRICE	开票价格	DECIMAL	19,2	Y
                xmlWriterBody.writeElementString("INVOICEPRICE",qry.reqInvoiceMsgDetail.InvoicePrice);
//                INVOICEDATE	开票日期	VARCHAR	10	Y
                xmlWriterBody.writeElementString("INVOICEDATE",qry.reqInvoiceMsgDetail.InvoiceDate);
//                INVOICENO	发票号码	VARCHAR	30	Y
                xmlWriterBody.writeElementString("INVOICENO",qry.reqInvoiceMsgDetail.InvoiceNo);
//                INVOICECODE	发票代码	VARCHAR	30	N
                xmlWriterBody.writeElementString("INVOICECODE",qry.reqInvoiceMsgDetail.InvoiceCode);
//                INVOICESTATUS	发票状态	VARCHAR	50	N	"1：填开作废 2：正数发票 3：负数发票"
                if("0".equals(qry.reqInvoiceMsgDetail.InvoiceStatus)) {
                    xmlWriterBody.writeElementString("INVOICESTATUS","3");
                }else if ("1".equals(qry.reqInvoiceMsgDetail.InvoiceStatus)){
                    xmlWriterBody.writeElementString("INVOICESTATUS", "0");
                }else if ("3".equals(qry.reqInvoiceMsgDetail.InvoiceStatus)){
                    xmlWriterBody.writeElementString("INVOICESTATUS", "1");
                }else{
                    xmlWriterBody.writeElementString("INVOICESTATUS", "");
                }
//                FIELD6	属性6	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD6","");
//                FIELD7	属性7	DECIMAL	19,2	N
                xmlWriterBody.writeElementString("FIELD7","");
//                FIELD8	属性8	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD8","");
//                FIELD9	属性9	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD9","");
//                FIELD10	属性10	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD10","");
                xmlWriterBody.writeEndElement();
            }
//                    LIST_END
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        retAts.respCode = "0";
                    }else{
                        retAts.respCode = "-1";
                    }
                    retAts.respInfo = retCode;
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
